package cn.com.blackboard.SISIntergrationBiz.controller;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import blackboard.base.BaseComparator;
import blackboard.base.GenericFieldComparator;
import cn.com.blackboard.DataModule4B2.BBDataUtils.LocaleUtil;
import cn.com.blackboard.DataModule4B2.MemoryCache.TeacherInfoMemCache;
import cn.com.blackboard.DataModule4B2.model.TeacherInfo;
import cn.com.blackboard.DataModule4B2.service.TeacherService;
import cn.com.blackboard.DataModule4B2.util.BatchResultModel;
import cn.com.blackboard.DataModule4B2.util.Condition;
  
@Controller
@RequestMapping("/teacher")
public class TeacherInfoController {  
	
	private static final Logger logger = LoggerFactory.getLogger(TeacherInfoController.class);

    @Autowired  
    private TeacherService teacherService;
    
    @RequestMapping("/getTeacherBycondition")  
    public String getUserByCondition(@RequestParam(value = "condition",required=false) String conditionJsonStr,
									    		ModelMap modelMap
									    		) throws IOException{
    	logger.info("getTeacherBycondition/jsonStr"+conditionJsonStr);
    	//logger.info(""+conList.size());
    	List<Condition> conList = Condition.ParseConditionList(conditionJsonStr);
    	List<TeacherInfo> list = teacherService.getUsers(conList);
		GenericFieldComparator useridComparator = new GenericFieldComparator( BaseComparator.ASCENDING, "getUserId", TeacherInfo.class );
		GenericFieldComparator getFirstNameComparator = new GenericFieldComparator( BaseComparator.ASCENDING, "getFirstName", TeacherInfo.class );
		GenericFieldComparator getDepartmentIdComparator = new GenericFieldComparator( BaseComparator.ASCENDING, "getDepartmentId", TeacherInfo.class );
		
		modelMap.addAttribute("useridComparator", useridComparator);
		modelMap.addAttribute("getFirstNameComparator", getFirstNameComparator);
		modelMap.addAttribute("getDepartmentIdComparator", getDepartmentIdComparator);
		modelMap.addAttribute("list", list);
		Locale locale = LocaleUtil.getLocale();
		modelMap.addAttribute("locale", locale.toString());
		
		if(!TeacherInfoMemCache.isEmpty())
			TeacherInfoMemCache.clear();
		TeacherInfoMemCache.cacheTeacherInfo(list);
		
		return "/import/importTeacher"; 
    }
    
    @RequestMapping(value = "importTeacher", method = {RequestMethod.POST },produces={"text/html;charset=UTF-8;","application/json;"}) 
    @ResponseBody  
    public String importTeacher(@RequestBody  List<String> keyList
									    		) throws IOException{
    	
    	logger.info("into import teacher");
    	logger.info("listsize = "+keyList.size());
    	List<TeacherInfo> list = TeacherInfoMemCache.getListByKeyList(keyList);
    	String result = "";
    	List<BatchResultModel> resultModelList = teacherService.importTeacher(list);
    	for(BatchResultModel rsModel:resultModelList){
    		result+=rsModel.getModelClsName()+"."+rsModel.getModelClsAttr()+":"+rsModel.getValue()+"-->"+rsModel.getStatus().getValue()+"\n";
    	}
		return result;
    }

    @RequestMapping(value = "batchImportTeacher", method = {RequestMethod.POST },produces={"text/html;charset=UTF-8;","application/json;"}) 
    @ResponseBody  
    public String batchImportTeacher() throws IOException{
    	
    	logger.info("into batch import teacher");
    	String result = "";
    	List<BatchResultModel> resultModelList = teacherService.batchImportTeacher();
    	for(BatchResultModel rsModel:resultModelList){
    		result+=rsModel.getModelClsName()+"."+rsModel.getModelClsAttr()+":"+rsModel.getValue()+"-->"+rsModel.getStatus().getValue()+"\n";
    	}
		return result;
    }
}  
